//
//  ViewController.m
//  tableview中键盘管理
//
//  Created by ios on 16/7/19.
//  Copyright © 2016年 ios. All rights reserved.
//

#import "ViewController.h"
#import "TableViewCell.h"
#import "iPadTableViewCell.h"

#define SCREEN_WIDTH ( [[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ( [[UIScreen mainScreen] bounds].size.height)



static NSString *cellID = @"cell";
static NSString *iPadcellID = @"iPadcell";

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *tabelView;
@end

@implementation ViewController

-(UITableView *)wTabelView{
    if (_tabelView==nil) {
        _tabelView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH , SCREEN_HEIGHT) style:UITableViewStyleGrouped];
        _tabelView.delegate = self;
        _tabelView.dataSource = self;
        [self.view addSubview:_tabelView];
        [_tabelView registerNib:[UINib nibWithNibName:@"TableViewCell" bundle:nil] forCellReuseIdentifier:cellID];
        
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad){
        [_tabelView registerNib:[UINib nibWithNibName:@"iPadTableViewCell" bundle:nil] forCellReuseIdentifier:iPadcellID];
        }else{
            NSLog(@"iphone or ipod");
        [_tabelView registerNib:[UINib nibWithNibName:@"TableViewCell" bundle:nil] forCellReuseIdentifier:cellID];
        }
    }
    return _tabelView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //self.view.backgroundColor = [UIColor redColor];
    
    [self.wTabelView reloadData];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    iPadTableViewCell *iPadcell = [tableView dequeueReusableCellWithIdentifier:iPadcellID];
    
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad){
        return iPadcell;
    }
    
    return cell;
}

-(void)viewDidAppear:(BOOL)animated{

    [super viewDidAppear:animated];
    [self beginObservingKeyboard];
    
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self endObservingKeyboard];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}

- (void)beginObservingKeyboard {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}

- (void)endObservingKeyboard {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
}

- (void)keyboardWillShow:(NSNotification *)note {
    CGRect keyboardRect = [[note.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(self.tabelView.contentInset.top, 0.0f, CGRectGetHeight(keyboardRect), 0.0f);
    self.tabelView.contentInset = contentInsets;
    self.tabelView.scrollIndicatorInsets = contentInsets;
}

- (void)keyboardDidHide:(NSNotification *)note {
     CGRect keyboardRect = [[note.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    UIEdgeInsets contentInset = UIEdgeInsetsMake(self.tabelView.contentInset.top, 0.0f, self.tabelView.contentInset.bottom-CGRectGetHeight(keyboardRect), 0.0f);
    
    [UIView animateWithDuration:0.25 animations:^{
        self.tabelView.contentInset = contentInset;
        self.tabelView.scrollIndicatorInsets = contentInset;
    }];
    
}

@end


